<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="80px">

      <el-form-item label="商户单号" prop="outTradeNo">
        <el-input
          v-model="queryParams.outTradeNo"
          placeholder="请输入商户单号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="商品名称" prop="goodName">
        <el-input
          v-model="queryParams.goodName"
          placeholder="请输入商品名称"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="支付渠道" prop="payMethod">
        <el-select v-model="queryParams.payMethod" placeholder="请选择支付渠道" clearable>
          <el-option
            v-for="dict in dict.type.payment_channel"
            :key="dict.value"
            :label="dict.label"
            :value="dict.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="状态" prop="status">
        <el-select v-model="queryParams.status" placeholder="请选择状态" clearable>
        <el-option
          v-for="dict in dict.type.order_status"
          :key="dict.value"
          :label="dict.label"
          :value="dict.value"
        />
      </el-select>
      </el-form-item>

      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
        >导出订单</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="uorderList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="商户单号" align="center" prop="outTradeNo" />
      <el-table-column label="商品名称" align="center" prop="goodName" />
      <el-table-column label="支付渠道" align="center" prop="payMethod">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.payment_channel" :value="scope.row.payMethod"/>
        </template>
      </el-table-column>
      <el-table-column label="付款金额(元)" align="center" prop="price" />
      <el-table-column label="交易时间" align="center" prop="transactionTime" />
      <el-table-column label="退款时间" align="center" prop="refundTime" />
      <el-table-column label="状态" align="center" prop="status">
        <template slot-scope="scope">
          <dict-tag :options="dict.type.order_status" :value="scope.row.status"/>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160px">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-document"
            @click="handleUpdate(scope.row)"
          >详情</el-button>

          <el-button
            v-show="scope.row.status == 2"
            size="mini"
            type="text"
            icon="el-icon-document-delete"
            @click="refund(scope.row)"
          >退款</el-button>

          <el-button
            v-show="scope.row.status == 2 || scope.row.status == 5"
            size="mini"
            type="text"
            icon="el-icon-position"
            @click="resendNotice(scope.row)"
          >通知</el-button>

          <el-button
            v-show="scope.row.status == 3"
            size="mini"
            type="text"
            icon="el-icon-position"
            @click="del(scope.row)"
          >删除</el-button>
        </template>
      </el-table-column>

    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改用户订单对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="450px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="商户单号:" prop="outTradeNo">
          <b>{{form.outTradeNo}}</b>
        </el-form-item>
        <el-form-item label="商品名称:" prop="goodName">
          <b>{{form.goodName}}</b>
        </el-form-item>
        <el-form-item label="支付方式:" prop="goodName">
          <dict-tag :options="dict.type.payment_channel" :value="form.payMethod" />
        </el-form-item>
        <el-form-item label="付款金额:" prop="price">
          <b>{{form.price}} 元</b>
        </el-form-item>
        <el-form-item label="交易时间:" prop="transactionTime">
          <b>{{form.transactionTime}}</b>
        </el-form-item>
        <el-form-item label="退款时间:" prop="refundTime">
          <b>{{form.refundTime}}</b>
        </el-form-item>
        <el-form-item label="创建时间:" prop="createTime">
          <b>{{form.createTime}}</b>
        </el-form-item>
        <el-form-item label="状态:" prop="status">
          <dict-tag :options="dict.type.order_status" :value="form.status" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="cancel">关 闭</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import { listUorder, getUorder, refund, orderNotice,delUorder } from "@/api/system/uorder";

export default {
  name: "Uorder",
  dicts: ['order_status', 'payment_channel'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户订单表格数据
      uorderList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        id:null,
        pageNum: 1,
        pageSize: 10,
        userId: null,
        appId: null,
        outTradeNo: null,
        tradeNo: null,
        refundNo: null,
        goodName: null,
        payMethod: null,
        price: null,
        payParam: null,
        refundParam: null,
        status: null,
        transactionTime: null,
        refundTime: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    if(this.$route.params.id == undefined){
      this.$modal.msgError("警告！请勿非法访问！");
    }else{
      this.queryParams.appId = this.$route.params.id;
      this.getList();
    }
  },
  methods: {
    /** 查询用户订单列表 */
    getList() {
      this.loading = true;
      listUorder(this.queryParams).then(response => {
        this.uorderList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        userId: null,
        appId: null,
        outTradeNo: null,
        tradeNo: null,
        refundNo: null,
        goodName: null,
        payMethod: null,
        price: null,
        payParam: null,
        refundParam: null,
        status: null,
        transactionTime: null,
        refundTime: null,
        createBy: null,
        createTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const id = row.id || this.ids
      getUorder(id).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "用户订单详情";
      });
    },
    refund(row) {
      this.queryParams.id = row.id;
      refund(this.queryParams).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("退款成功！");
        }else{
          this.$modal.msgError(response.message);
        }
        this.getList();
      });
    },
    resendNotice(row){
      this.queryParams.id = row.id;
      orderNotice(this.queryParams).then(response => {
          if(response.code == 200){
            this.$modal.msgSuccess("重发通知成功！");
          }else{
            this.$modal.msgError(response.message);
          }
      });
    },
    del(row){
      delUorder(row.id).then(response => {
        if(response.code == 200){
          this.$modal.msgSuccess("删除成功！");
          this.getList();
        }else{
          this.$modal.msgError(response.message);
        }
      });
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('system/uorder/export', {
        ...this.queryParams
      }, `uorder_${new Date().getTime()}.xlsx`)
    }
  }
};
</script>
